package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.TbItemCatService;
import com.dongdongshop.service.TbTypeTemplateService;
import org.omg.PortableInterceptor.LOCATION_FORWARD;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
@Controller
@RequestMapping("tbItemCat")
public class TbItemCatController {
    @Reference
    private TbItemCatService tbItemCatService;
    @Reference
    private TbTypeTemplateService tbTypeTemplateService;
    @RequestMapping("toTbItemCat")
    public String  toTb(Model model){
        List<TbTypeTemplate> tbTypeTemplates = tbTypeTemplateService.selectAll();
        model.addAttribute("typeList",tbTypeTemplates);
        return "admin/item_cat";
    }
    @RequestMapping("findItem")
    @ResponseBody
    public List<TbItemCat> fidItem(@RequestParam(defaultValue = "0")Long  pid){
        List<TbItemCat> tbItemCatList =  tbItemCatService.selectById(pid);
        return tbItemCatList;
    }
    @RequestMapping("toAddItem")
    @ResponseBody
    public Result toAdd(TbItemCat tbItemCat){
        tbItemCatService.addAll(tbItemCat);
        return Result.build(ConstantEnum.SUCCESS);
    }
    //回显
    @RequestMapping("echoItem/{id}")
    @ResponseBody
    public TbItemCat toEcho(@PathVariable("id")Long id){
         TbItemCat tbItemCat =  tbItemCatService.findItemById(id);
         return  tbItemCat;
    }
    //修改
    @ResponseBody
    @RequestMapping("updateItem")
    public Result toUpdate(TbItemCat tbItemCat){
        if (tbItemCat==null){
        return Result.build(ConstantEnum.ARCS_NULL);
    }
        tbItemCatService.updateItem(tbItemCat);
        return Result.build(ConstantEnum.SUCCESS);
    }
    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result toDelete(Long[] ids){
        tbItemCatService.deleteBatch(ids);
        return Result.build(ConstantEnum.SUCCESS);
    }

}
